#!/usr/bin/env python3
import os
import sys
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wealth_backend.settings')
django.setup()

from django.db import connection

def check_table_structure(table_name):
    """查看表结构"""
    cursor = connection.cursor()
    
    try:
        # 首先检查表是否存在
        cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
        if not cursor.fetchone():
            print(f"表 {table_name} 不存在")
            return
            
        cursor.execute(f'DESCRIBE {table_name}')
        columns = cursor.fetchall()
        
        print(f"\n表 {table_name} 的结构:")
        print("-" * 80)
        print(f"{'字段名':<25} {'数据类型':<20} {'允许NULL':<10} {'键':<10} {'默认值':<15} {'额外信息'}")
        print("-" * 80)
        
        for col in columns:
            field_name = col[0]
            data_type = col[1]
            null_allowed = col[2]
            key = col[3]
            default_value = col[4] if col[4] is not None else ''
            extra = col[5] if len(col) > 5 else ''
            
            print(f"{field_name:<25} {data_type:<20} {null_allowed:<10} {key:<10} {default_value:<15} {extra}")
            
    except Exception as e:
        print(f"查询表 {table_name} 时出错: {e}")
    finally:
        cursor.close()

if __name__ == '__main__':
    # 查看fund_NetAssetValue表结构
    check_table_structure('fund_NetAssetValue')